package cn.edu.gxu.oas.module.run.service.record;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.edu.gxu.oas.module.run.controller.admin.record.vo.*;
import cn.edu.gxu.oas.module.run.dal.dataobject.record.RecordDO;
import cn.edu.gxu.oas.framework.common.pojo.PageResult;
import cn.edu.gxu.oas.framework.common.util.object.BeanUtils;
import cn.edu.gxu.oas.framework.mybatis.core.query.LambdaQueryWrapperX;

import cn.edu.gxu.oas.module.run.dal.mysql.record.RecordMapper;

import java.time.LocalDateTime;
import java.util.List;

import static cn.edu.gxu.oas.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.edu.gxu.oas.module.run.enums.ErrorCodeConstants.*;

/**
 * 跑步记录 Service 实现类
 *
 * @author 源码
 */
@Service
@Validated
public class RunRecordServiceImpl implements RunRecordService {

    @Resource
    private RecordMapper recordMapper;

    @Override
    public Long createRecord(RecordSaveReqVO createReqVO) {
        // 插入
        RecordDO record = BeanUtils.toBean(createReqVO, RecordDO.class);
        recordMapper.insert(record);
        // 返回
        return record.getId();
    }

    @Override
    public void updateRecord(RecordSaveReqVO updateReqVO) {
        // 校验存在
        validateRecordExists(updateReqVO.getId());
        // 更新
        RecordDO updateObj = BeanUtils.toBean(updateReqVO, RecordDO.class);
        recordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecord(Long id) {
        // 校验存在
        validateRecordExists(id);
        // 删除
        recordMapper.deleteById(id);
    }

    private void validateRecordExists(Long id) {
        if (recordMapper.selectById(id) == null) {
            throw exception(RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RecordDO getRecord(Long id) {
        return recordMapper.selectById(id);
    }

    @Override
    public PageResult<RecordDO> getRecordPage(RecordPageReqVO pageReqVO) {
        return recordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RecordDO> getRecordList(RecordPageReqVO reqVO) {
        return recordMapper.selectList(new LambdaQueryWrapperX<RecordDO>()
                .eqIfPresent(RecordDO::getStudentId, reqVO != null ? reqVO.getStudentId() : null)
                .betweenIfPresent(RecordDO::getStartTime, reqVO != null ? reqVO.getStartTime() : null)
                .betweenIfPresent(RecordDO::getEndTime, reqVO != null ? reqVO.getEndTime() : null)
                .eqIfPresent(RecordDO::getStatus, reqVO != null ? reqVO.getStatus() : null)
                .orderByDesc(RecordDO::getId));
    }

    @Override
    public List<RecordDO> getRecordListByStudentId(Long studentId) {
        return recordMapper.selectList(new LambdaQueryWrapperX<RecordDO>()
                .eq(RecordDO::getStudentId, studentId)
                .orderByDesc(RecordDO::getId));
    }

    @Override
    public List<RecordDO> getRecordsByStudentIdAndTimeRange(Long studentId, LocalDateTime startTime, LocalDateTime endTime) {
        return recordMapper.selectList(new LambdaQueryWrapperX<RecordDO>()
                .eq(RecordDO::getStudentId, studentId)
                .between(RecordDO::getStartTime, startTime, endTime)
                .orderByDesc(RecordDO::getId));
    }
}